package com.liaoban.app.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.liaoban.app.R;
import com.liaoban.app.api.type.event.ShareTixianSucEvent;
import com.liaoban.app.api.type.event.TixianSucEvent;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.base.BaseToolbarActivity;
import com.liaoban.app.util.CommonUtils;
import com.liaoban.app.widget.MyAlertDialog;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ybc on 2018/8/9 0009.
 * 我的收益
 */
public class MyEarnActivity extends BaseToolbarActivity {
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_one)
    TextView tvOne;
    @BindView(R.id.tv_two)
    TextView tvTwo;
    @BindView(R.id.tv_three)
    TextView tvThree;
    @BindView(R.id.tv_four)
    TextView tvFour;
    @BindView(R.id.tv_five)
    TextView tvFive;
    @BindView(R.id.tv_liao_b)
    TextView tvLiaoB;
    @BindView(R.id.tv_keyi_money)
    TextView tvKeYiMoney;

    @Override
    protected String getNewTitle() {
        return getString(R.string.my_earn);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_my_earn;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setNeedOnBus(true);
        tvRight.setText("明细");
        tvRight.setVisibility(View.VISIBLE);
        List<String> parts1 = new ArrayList<>();
        parts1.add("1");
        parts1.add("10");
        CommonUtils.modifyMultiColor(tvOne, "聊币与人民币的兑换比例为10聊币=1RMB", parts1, getResources().getColor(R.color.text_black));
        List<String> parts2 = new ArrayList<>();
        parts2.add("9:30-18:00");
        CommonUtils.modifyMultiColor(tvTwo, "每天早上9:30-18:00的提现当天到账，其他时间隔天到账", parts2, getResources().getColor(R.color.text_black));
        List<String> parts3 = new ArrayList<>();
        parts3.add("100");
        CommonUtils.modifyMultiColor(tvThree, "余额需要大约100元才能进行提现", parts3, getResources().getColor(R.color.text_black));
        loadData();
    }

    private void loadData() {
        showRunningDialog();
        startTask(CommonBiz.getInstance().profit(), profitBaseResponse -> {
            tvLiaoB.setText(profitBaseResponse.data.getLiao_b() + "");
            tvKeYiMoney.setText(profitBaseResponse.data.getKeyi_money() + "");
        });
    }

    @OnClick(R.id.tv_right)
    public void right() {
        startActivity(new Intent(this, EarnListActivity.class));
    }

    @OnClick(R.id.tv_withdraw)
    public void withDraw() {
        showRunningDialog();
        startTask(CommonBiz.getInstance().tixianStatus(), objectBaseResponse -> {
            if (2 == objectBaseResponse.code) {
                new MyAlertDialog(this).builder().setMsg(getString(R.string.un_bind_pay_tip)).
                        setPositiveButton("绑定支付宝", view1 -> startActivity(new Intent(this, BindPayActivity.class))).
                        setNegativeButton("", null).show();
            } else if (0 == objectBaseResponse.code) {
                ToastHelper.show(this, objectBaseResponse.msg);
            } else {
                startActivity(WithDrawActivity.newIntent(this, WithDrawActivity.TYPE_TIXIAN, objectBaseResponse.data.getKeyi_money() + "", objectBaseResponse.data.getAlipay_name()));
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTixianSucEvent(TixianSucEvent tixianSucEvent) {
        loadData();
    }
}
